/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: agent-java
 * Description: Agent written in java language
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.agent;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.springframework.util.StringUtils;

import ru.ffind.agent.net.NetTools;
import ru.ffind.common.model.HostType;

/**
 * Configuration utility class for java-agent
 * Implemented as singleton
 *
 * @author Rage Steel
 */
public final class AgentConfigImpl implements AgentConfig {
	
	private final static Logger LOGGER =
		Logger.getLogger(AgentConfigImpl.class.getName());
	
	public static final AgentConfig INSTANCE = new AgentConfigImpl();
	
	private final Properties properties;
	
	private void loadProperties(String filename) {
		String configFilename = "cfg/" + filename + ".properties";
		try {
			Reader reader = new FileReader(configFilename);
			properties.load(reader);
		} catch (IOException e) {
			// Nothing to do - use defaults
			LOGGER.log(Level.WARNING, "Error loading config file!", e);
		}
	}
	
	/**
	 * Creates configuration object, loading configuration files
	 */
	private AgentConfigImpl() {
		super();
		
		properties = new Properties();
		
		loadProperties("agent");
		loadProperties("agent-" + NetTools.HOSTNAME);
	}
	
	/**
	 * Return string property 
	 * @param key Key to property
	 * @param defaultValue Default value of property
	 * @return
	 */
	private String getString(String key, String defaultValue) {
		return properties.getProperty(key, defaultValue);
	}
	
	/**
         * {@inheritDoc}
	 */
        @Override
	public String getDbUrl() {
		return getString("db.url", "jdbc:mysql://localhost/ffind");
	}
	
	/**
         * {@inheritDoc}
	 */
        @Override
	public String getDbUsername() {
		return getString("db.username", "root");
	}
	
	/**
     * {@inheritDoc}
	 */
        @Override
	public String getDbPassword() {
		return getString("db.password", "");
	}
	
	/**
     * {@inheritDoc}
	 */
        @Override
	public String getDbDriverClassname() {
		return getString("db.driver.classname", "com.mysql.jdbc.Driver");
	}
	
	/**
     * {@inheritDoc}
	 */
        @Override
	public File[] getRoots() {
		String rootsStr = getString("roots", null);
		if (null == rootsStr) {
			return File.listRoots();
		}
		
		String[] rootsArr = StringUtils.delimitedListToStringArray(rootsStr, File.pathSeparator);
		File[] result = new File[rootsArr.length];
		for (int i = 0; i < rootsArr.length; i++) {
			result[i] = new File(rootsArr[i]);
		}
		return result;
	}

	/**
     * {@inheritDoc}
	 */
    @Override
    public HostType getHostType() {
        String hostTypeStr = getString("host.type", "SHARE");
        return HostType.valueOf(hostTypeStr);
    }

	/**
     * {@inheritDoc}
	 */
    @Override
    public String getHostOwner() {
        return getString("host.owner", "UNKNOWN");
    }
}
